package com.dikang.keer.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.R;
import com.dikang.keer.product.entity.AttrEntity;
import com.dikang.keer.product.service.AttrAttrgroupRelationService;
import com.dikang.keer.product.service.AttrService;
import com.dikang.keer.product.service.CategoryService;
import com.dikang.keer.product.vo.AttrGroupVo;
import com.dikang.keer.product.vo.AttrRespVo;
import com.dikang.keer.product.vo.GroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.dikang.keer.product.entity.AttrGroupEntity;
import com.dikang.keer.product.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author stbz
 * @email 1287796639@qq.com
 * @date 2021-10-21 02:01:46
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;
    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId){
        PageUtils page = attrGroupService.queryPage(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		attrGroup.setCatelogPath(categoryService.getcatelogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrRelation = attrGroupService.getAttrRelation(attrgroupId);
        return R.ok().put("data",attrRelation);
    }
    @PostMapping("attr/relation/delete")
    public R removeAttrRelation(@RequestBody AttrGroupVo[] attrGroupVos){
        attrGroupService.deleteAttrGroup(attrGroupVos);
        return R.ok();
    }
    @RequestMapping("{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils pageUtils = attrService.getNoattrRelation(params, attrgroupId);
        return R.ok().put("page",pageUtils);
    }
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrRespVo> attrRespVos){
        attrgroupRelationService.saveAttrRelation(attrRespVos);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getGroupwithAttr(@PathVariable("catelogId") Long catelogId){
        List<GroupWithAttrsVo> groupWithAttrs = attrGroupService.getGroupwithAttrByCatelogId(catelogId);
        System.out.println(groupWithAttrs);
        return R.ok().put("data",groupWithAttrs);
    }


}
